#!/bin/bash
# Copyright 2024 Hangzhou Yingyi Technology Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

source $PWD/util.sh

parse_boards_info

export DEF_VERSION="release-v0.1.0"

while [[ $# -gt 0 ]]; do
  options="$1"
  case $options in
  --help | -h)
    print_help
    exit 0
    ;;
  --support-boards | -s)
    print_boards
    exit 0
    ;;
  --init-libs=*)
    init_libs $(readlink -f "${options#*=}")
    exit 0
    ;;
  --update-libs=*)
    update_libs $(readlink -f "${options#*=}")
    exit 0
    ;;
  --board=* | -b=*)
    BOARD_NAME=${options#*=}
    ;;
  --plat=* | -p=*)
    PLAT_NAME=${options#*=}
    ;;
  --kernel=* | -k=*)
    KERNEL_PATH=$(readlink -f "${options#*=}")
    ;;
  --app=* | -a=*)
    APP_PATH=$(readlink -f "${options#*=}")
    ;;
  --config=* | -c=*)
    CONF_PATH=$(readlink -f "${options#*=}")
    ;;
  distclean | clean | pack)
    if [ -z "$COMMAND" ]; then
      COMMAND=$options
    else
      echo "Repeat command argument, please check and retry."
      exit 1
    fi
    ;;
  *)
    echo "unknown parameter $options"
    printf "%s\n" "$BUILD_HELP_MESSAGE"
    exit 1
    ;;
  esac
  shift
done

if [ -z "$KERNEL_PATH" ]; then
  KERNEL_PATH=$(readlink -f "../tenon")
fi

if [ -z "$BOARD_NAME" ] && [ -z "$PLAT_NAME" ]; then
  echo "No valid board name or platform name was provided."
  echo "Use -b for the board name or -p for the platform name."
  print_boards
  printf "%s\n" "$BUILD_HELP_MESSAGE"
  exit 1
fi

TARGET_NAME=${BOARD_NAME:-${PLAT_NAME}}
check_board $TARGET_NAME
BOARD_PATH=$PWD/$TARGET_NAME
PLAT_NAME=$(echo "${Supported_Boards[$TARGET_NAME]}" | jq -r '.plat')

LIBS_PATH=$(readlink -f "../libs")
PLATS_PATH=$(readlink -f "../plats")
DRIVERS_PATH=$(readlink -f "../drivers")
BUILD_PATH=$(readlink -f "./build")
if [ -z "$APP_PATH" ]; then
  APP_PATH=$KERNEL_PATH
fi

case "$COMMAND" in

distclean)
  make -C $KERNEL_PATH distclean O=$BUILD_PATH P=$PLATS_PATH D=$DRIVERS_PATH L=$LIBS_PATH A=$APP_PATH
  rm -rf $BOARD_PATH/build
  exit 0
  ;;
clean)
  make -C $KERNEL_PATH clean O=$BUILD_PATH P=$PLATS_PATH D=$DRIVERS_PATH L=$LIBS_PATH A=$APP_PATH
  rm -rf $BOARD_PATH/build
  exit 0
  ;;
pack) ;;
*)
  if [ -z "$CONF_PATH" ]; then
    cp $BOARD_PATH/defconfig $APP_PATH/.config
  else
    cp $CONF_PATH $APP_PATH/.config
  fi
  make -C $KERNEL_PATH menuconfig O=$BUILD_PATH P=$PLATS_PATH D=$DRIVERS_PATH L=$LIBS_PATH A=$APP_PATH
  source $APP_PATH/.config
  SELECT_PLAT="CONFIG_PLAT_$(echo $PLAT_NAME | tr '[:lower:]' '[:upper:]')"
  if [ "${!SELECT_PLAT}" != y ]; then
    echo "ERROR: Configuration platform conflicts with packaged board, please check and retry."
    exit 1
  fi
  echo "-----------------------------------------------------------"
  echo
  echo "**The generated config file is stored in the $BUILD_PATH.**"
  echo
  echo "-----------------------------------------------------------"
  make -C $KERNEL_PATH -j $(nproc) O=$BUILD_PATH P=$PLATS_PATH D=$DRIVERS_PATH L=$LIBS_PATH A=$APP_PATH > /dev/null 2>&1
  if [ $? != 0 ]; then
    echo "ERROR: Building kernel image failed!"
    exit 1
  fi
  ;;
esac

#执行打包操作
PACKAGE_SCRIPTS=$PWD/pack.sh
bash "$PACKAGE_SCRIPTS" $BOARD_PATH $KERNEL_PATH $BUILD_PATH
